package com.zgc.zkinfo;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.jfoenix.svg.SVGGlyph;
import com.zgc.cache.ConnectionInfoCache;
import com.zgc.view.svg.SvgIconEnum;
import javafx.beans.binding.When;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.scene.paint.Color;
import org.springframework.util.DigestUtils;

public class ZkConnectionInfo {

    private String id;
    private String name;
    private String host;
    private int connectionTimeoutMs;
    private int sessionTimeoutMs;
    private int connectionWaitTimeout;
    @JsonIgnore()
    private final SimpleBooleanProperty connected = new SimpleBooleanProperty(this, "connectedStat", false);


    public ZkConnectionInfo() {
    }

    public String getId() {
        return id = DigestUtils.md5DigestAsHex(host.getBytes());
    }

    public String getName() {
        return name;
    }

    public ZkConnectionInfo setName(String name) {
        this.name = name;
        return this;
    }

    @JsonIgnore()
    public SVGGlyph getStatIcon() {
        SVGGlyph statIcon = SvgIconEnum.ICON_ZK_STAT.init(Color.DIMGRAY, 16, 16);
        statIcon.fillProperty().bind(new When(connected).then(Color.valueOf("#4D66CC")).otherwise(Color.DIMGRAY));
        return statIcon;
    }

    public String getHost() {
        return host;
    }

    public ZkConnectionInfo setHost(String host) {
        this.host = host;
        return this;
    }

    public int getConnectionTimeoutMs() {
        return connectionTimeoutMs;
    }

    public ZkConnectionInfo setConnectionTimeoutMs(int connectionTimeoutMs) {
        this.connectionTimeoutMs = connectionTimeoutMs;
        return this;
    }

    public int getSessionTimeoutMs() {
        return sessionTimeoutMs;
    }

    public ZkConnectionInfo setSessionTimeoutMs(int sessionTimeoutMs) {
        this.sessionTimeoutMs = sessionTimeoutMs;
        return this;
    }

    public int getConnectionWaitTimeout() {
        return connectionWaitTimeout;
    }

    public ZkConnectionInfo setConnectionWaitTimeout(int connectionWaitTimeout) {
        this.connectionWaitTimeout = connectionWaitTimeout;
        return this;
    }

    public boolean connected() {
        return connected.get();
    }

    public ZkConnectionInfo setConnected(boolean connected) {
        this.connected.set(connected);
        return this;
    }

    public SimpleBooleanProperty connectedProperty() {
        return connected;
    }

    /**
     * 提交到 ZkConnectProperty
     */
    public ZkConnectionInfo commit() {
        ConnectionInfoCache.putConnectInfo(getId(), this);
        return this;
    }


    @Override
    public String toString() {
        return "ZkConnectionInfo{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", host='" + host + '\'' +
                ", connectionTimeoutMs=" + connectionTimeoutMs +
                ", sessionTimeoutMs=" + sessionTimeoutMs +
                ", connectionWaitTimeout=" + connectionWaitTimeout +
                ", isConnect=" + connected() +
                '}';
    }
}
